package com.api.aiyh_pcn.copy_attachment.service;

import aiyh.utils.ApiResult;
import aiyh.utils.zwl.common.ToolUtil;
import com.api.aiyh_pcn.copy_attachment.dao.DocTemplateDao;
import com.api.workflow.constant.RequestAuthenticationConstant;
import com.engine.workflow.biz.requestForm.FileBiz;
import weaver.general.Util;
import weaver.hrm.User;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @author EBU7-dev1-ayh
 * @create 2021/8/27 0027 14:52
 * copyservice
 */


public class CopyAttachmentService {
    public static String copyFile(User user, Map<String, Object> params) {
        ToolUtil toolUtil = new ToolUtil();
//		建模表名称
        String tableName = String.valueOf(params.get("tableName"));
//		建模字段名
        String fieldName = String.valueOf(params.get("fieldName"));
//		附件字段id
        String fileFieldId = String.valueOf(params.get("fileFieldId"));
//		配置id
        String configId = String.valueOf(params.get("configId"));
//		流程id
        String workflowId = String.valueOf(params.get("workflowId"));
        DocTemplateDao docTemplateDao = new DocTemplateDao();
        int[] docIds = null;
        try {
//			docIds = docTemplateDao.copyFile(user.getUID(), tableName, fieldName, configId);
            docIds = docTemplateDao.copyFile(user, tableName, fieldName, configId, workflowId, fileFieldId);
        } catch (Exception e) {
            toolUtil.writeErrorLog("复制文件出错： " + e);
            return ApiResult.error("复制文件出错！");
        }
        if (docIds == null) {
            return ApiResult.error("未查询到附件模板！");
        }
		/*try{
			docTemplateDao.updateFileInfo(workflowId,fileFieldId,docIds);
		}catch(Exception e){
		   toolUtil.writeErrorLog("复制文件更新权限和目录出错： " + e);
			return null;
		}*/
        toolUtil.writeDebuggerLog("数据更新成功！" + Arrays.toString(docIds));
        return ApiResult.success(docIds);
    }

    public static String deleteFile(String docIdStr) {
        String[] docIds = docIdStr.split(",");
        DocTemplateDao docTemplateDao = new DocTemplateDao();
        int[] array = Arrays.stream(docIds).mapToInt(Integer::parseInt).toArray();
        try {
            docTemplateDao.deleteFile(array);
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResult.error("删除失败！");
        }
        return ApiResult.success(array, "删除成功！");
    }

    /**
     * 查询文档信息
     *
     * @param user   user对象
     * @param params 前端参数
     * @return 文档信息
     */
    public static String queryFilesData(User user, Map<String, Object> params) {
        String listType = Util.null2String(String.valueOf(params.get("listType")), "list");
        int requestid = Util.getIntValue(Util.null2String(String.valueOf(params.get("requestid"))), -1);
        int desrequestid = Util.getIntValue(Util.null2String(String.valueOf(params.get("desrequestid"))), -1);
        int isprint = Util.getIntValue(Util.null2String(String.valueOf(params.get("isprint"))), 0);
        int workflowid = Util.getIntValue(Util.null2String(String.valueOf(params.get("workflowid"))), 0);
        String f_weaver_belongto_userid = Util.null2String(String.valueOf(params.get("f_weaver_belongto_userid")));
        String f_weaver_belongto_usertype = Util.null2String(String.valueOf(params.get("f_weaver_belongto_usertype")));
        String authStr = Util.null2String(String.valueOf(params.get(RequestAuthenticationConstant.AUTHORITY_STRING)));
        String authSignatureStr = Util.null2String(String.valueOf(params.get(RequestAuthenticationConstant.AUTHORITY_SIGNATURESTRING)));
        Map<String, Object> paramsMap = new HashMap<>();
        paramsMap.put("user", user);
        Map<String, Object> retobj;
        try {
            retobj = FileBiz.getFileDatas(Util.null2String(params.get("docIds")), listType, requestid, desrequestid,
                    isprint, f_weaver_belongto_userid, f_weaver_belongto_usertype, true, false, authStr, authSignatureStr, paramsMap);
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResult.error("查询附件信息失败：" + e);
        }
        return ApiResult.success(retobj);
    }
}
